<!DOCTYPE html>
<html>
    
    <head>
        
        <meta charset="utf-8">
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="keywords" content="" />
        <title>宿舍信息页面</title>
        
        <!-- ========== Css Files ========== -->
        <link href="./static/css/root.css" rel="stylesheet">
        
    </head>
    
    
    <body>
        <div class="login-form" style="width: 800px">
            <form method="post">
                <div class="top">
                    <h1>宿舍信息页面</h1>
                </div>
            </form>
        </div>
 
        <div class="container-padding container-widget" style="width: 790px; margin:0 auto">
            <div class="row">
                <div class="col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-title" id="stuName"> 个人信息 </div>

                        <div class="panel-body table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                          <td>学号</td>
                                          <td>性别</td>
                                          <td>当前是否可以选宿</td>
                                          <td>邀请码</td>
                                          <td>宿舍信息</td>
                                    </tr>
                              </thead>
                                  
                              <tbody>
                                  <tr>
                                      <td id="stuNum"> </td>
                                      <td id="stuSex"> </td>
                                      <td id="stuCan"> </td>
                                      <td id="veriNum"> </td>
                                      <td id="stuRom"> </td>
                                  </tr>
                              </tbody>
                            </table>
                        </div>
                    </div>

                    <div class="panel panel-default">
                        <div class="panel-title" id="stuName"> 宿舍查询 </div>

                        <div class="panel-body">
                            <form class="form-horizontal">
                                <div class="form-group">
                                    <div class="col-sm-1"></div>
    
                                    <label class="col-sm-2 control-label form-label" style="font-size: 120%;">请选择楼号：</label>
    
                                    <div class="col-sm-1"></div>
    
                                    <div class="col-sm-4">
                                        <select class="selectpicker" style="width: 100%;" id="numSel"> </select>
                                    </div>
    
                                    <div class="col-sm-1"></div>
    
                                    <button type="button" class="btn col-sm-2" style="height: 80%" onclick="search()">查询</button>
                                </div>
                            </form>
                        </div>

                    </div>

                    <div class="panel panel-default">
                        <div class="panel-title" id="stuName"> 绑定选宿 </div>

                        <div class="panel-body">
                            <div class="form-group col-sm-4">
                                <input type="text" class="form-control" id="stuID_1" placeholder="学号">
                                <!--<input type="text" class="form-control" id="stuID_1" placeholder="学号" value="2103215219">-->
                            </div>

                            <div class="form-group col-sm-4">
                                <input type="email" class="form-control" id="stuVeri_1" placeholder="邀请码">
                                <!--<input type="email" class="form-control" id="stuVeri_1" placeholder="邀请码" value="654321">-->
                            </div>

                            <div class="col-sm-1"></div>

                            <button type="button" class="btn col-sm-2" onclick="check_1()">检查</button>
                        </div>

                        <div class="panel-body">
                            <div class="form-group col-sm-4">
                                <input type="text" class="form-control" id="stuID_2" placeholder="学号">
                                <!--<input type="text" class="form-control" id="stuID_2" placeholder="学号" value="2101582168">-->
                            </div>

                            <div class="form-group col-sm-4">
                                <input type="email" class="form-control" id="stuVeri_2" placeholder="邀请码">
                                <!--<input type="email" class="form-control" id="stuVeri_2" placeholder="邀请码" value="123456">-->
                            </div>

                            <div class="col-sm-1"></div>

                            <button type="button" class="btn col-sm-2" onclick="check_2()">检查</button>
                        </div>

                        <div class="panel-body">
                            <div class="form-group col-sm-4">
                                <input type="text" class="form-control" id="stuID_3" placeholder="学号">
                            </div>

                            <div class="form-group col-sm-4">
                                <input type="email" class="form-control" id="stuVeri_3" placeholder="邀请码">
                            </div>

                            <div class="col-sm-1"></div>

                            <button type="button" class="btn col-sm-2" onclick="check_3()">检查</button>
                        </div>

                        <button type="button" class="btn btn-default btn-block" onclick="submitForm()" id="affirm">确认选择</button>
                        
                    </div>
                </div>
            </div>
            
            <div class="footer-links row">
                <div class="col-xs-6"><a href="#" onclick="turnToLogout()"><i class="fa fa-sign-in"></i>退出登录</a></div>
            </div>
        </div>
        
        
        <script type="text/javascript">
            function getBJtime() {
                var w_array=new Array("星期天","星期一","星期二","星期三","星期四","星期五","星期六");
                var d=new Date();
                var year=d.getFullYear();
                var month=d.getMonth()+1;
                var day=d.getDate();
                var week=d.getDay();
                var h=d.getHours();
                var mins=d.getMinutes();
                var s=d.getSeconds();
                if(month<10) month="0" + month
                if(day<10) month="0" + day
                if(h<10) h="0" + h
                if(mins<10) mins="0" + mins
                if(s<10) s="0" + s
                var shows= year + "-" + month + "-" + day + " " + h + ":" + mins + ":" + s + " " + w_array[week] + "";
                return shows;
            }
        
            async function submitForm() {
                var token = localStorage.getItem("token");
                
                var obj = document.getElementById("numSel");
                var index = obj.selectedIndex;
                const value = obj.options[index].value;  // 下拉框选中的值
                
                var stuArr = new Array();
                
                var stuID = document.getElementById("stuID_1").value;
                var stuVeri = document.getElementById("stuVeri_1").value;
                if (stuID && stuVeri) {
                    var arr = new Array();
                    arr.push(stuID); arr.push(stuVeri);
                    stuArr.push(arr);
                }
                var stuID = document.getElementById("stuID_2").value;
                var stuVeri = document.getElementById("stuVeri_2").value;
                if (stuID && stuVeri) {
                    var arr = new Array();
                    arr.push(stuID); arr.push(stuVeri);
                    stuArr.push(arr);
                }
                var stuID = document.getElementById("stuID_3").value;
                var stuVeri = document.getElementById("stuVeri_3").value;
                if (stuID && stuVeri) {
                    var arr = new Array();
                    arr.push(stuID); arr.push(stuVeri);
                    stuArr.push(arr);
                }
                var bjTime = getBJtime();
                console.log("北京时间:" + bjTime);
                console.log(stuArr);

                const res = await fetch('/list/submitInfo', {
            		method: 'POST',
            		body: JSON.stringify({ stuArr, value, bjTime, token }),
            		headers: {
            			'Content-Type': 'application/json'
            		}
            	});
            	
            	const json = await res.json();
            	const { state } = json;
            	console.log(state);
            	
            	if (state == 'STUERR') {
            	    swal("选宿舍失败！", "您输入的绑定学生信息有误，请点击检查！", "error");
            	} else if (state == 'SUCCESS') {
            	    swal({ title: "选宿舍成功！", text: "您的页面即将刷新！", type: "success" }, function() { location.reload(); });
            	} else if (state == 'CANERR') {
            	    swal("选宿舍失败！", "存在无选宿舍资格的用户！", "error");
            	} else if (state == 'FULL') {
            	    swal("选宿舍失败！", "所选楼宿舍已满！", "error");
            	} else if (state == 'CHECERR') {
            	    swal("错误！", "已选宿舍后无法再查询信息！", "error");
            	} else if (state == 'LOGERR') {
            	    swal("登录状态错误！", "请重新登录！", "error");
            	}
            }
        
            function getParams(key) {
                var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) {
                    return unescape(r[2]);
                }
                return null;
            };
            
            async function whenComeIn() {
                // 1. 添加个人信息
                var token = localStorage.getItem("token");
                
                const res = await fetch('/list/showInfo', {
            		method: 'POST',
            		body: JSON.stringify({ token }),
            		headers: {
            			'Content-Type': 'application/json'
            		}
            	});
            	
            	const json = await res.json();
            	const { stuID, stuName, stuSex, can_choose, buildToChoose, buidInfo, roomName, veriNum } = json;
            	
            	if (stuID == '') {
            	    return swal( {title: "登录产生错误！", text: "请尝试重新登录！", type: "error"}, function() {
            	        window.location.href="http://fanggx.xyz";
            	    });
            	}
            	console.log(buidInfo)
            	
            	document.getElementById("stuName").innerHTML = "个人信息（" + stuName + "）";
            	document.getElementById("stuNum").innerHTML = stuID;
            	document.getElementById("stuSex").innerHTML = stuSex;
            	document.getElementById("stuCan").innerHTML = can_choose;
            	document.getElementById("stuRom").innerHTML = roomName;
            	document.getElementById("veriNum").innerHTML = veriNum;
            	
            	if (can_choose == '无法选宿') {
            	    document.getElementById("affirm").disabled="true";
            	}
            	
            	// 2.添加下拉栏
            	var selectObj = document.getElementById("numSel");
            	for (var i=0; i < buidInfo.length; i++) {
            	    selectObj.options.add(new Option(buidInfo[i][0] + buidInfo[i][1], buidInfo[i][0] + buidInfo[i][1]));
            	};
            	
            };
            window.onload = whenComeIn;
            
            async function turnToLogout() {
                var token = localStorage.getItem("token");
                
                const res = await fetch('/list/logout', {
            		method: 'POST',
            		body: JSON.stringify({ token }),
            		headers: {
            			'Content-Type': 'application/json'
            		}
            	})
            	
            	localStorage.removeItem('token');
            	
            	const json = await res.json()
				console.log(json)
				const { msg } = json
				
				if (msg) {  // 退出成功
				    window.location.href="http://fanggx.xyz"
				} else {
				    window.location.href="http://fanggx.xyz"
				}
            }
            
            async function search() {
                var token = localStorage.getItem("token");
                
                var obj = document.getElementById("numSel");
                var index = obj.selectedIndex;
                const value = obj.options[index].value;  // 下拉框选中的值
                
                const res = await fetch('/list/searchNum', {
            		method: 'POST',
            		body: JSON.stringify({ token, value }),
            		headers: {
            			'Content-Type': 'application/json'
            		}
            	});
            	
            	const json = await res.json();
				console.log(json);
				const { totalNum } = json;
				
            	if ( totalNum ) {
            	    swal("宿舍情况查询！", "当前楼内剩余空床数为：" + totalNum);
            	} else {
            	    swal("查询失败！", "请检查是否登录过期！)", "error");
            	}
            	
            }
            
            async function check_1() {
                var token = localStorage.getItem("token");
                
                const stuID = document.getElementById("stuID_1").value;
                const stuVeri = document.getElementById("stuVeri_1").value;
                console.log(stuID, stuVeri);
                
                if (stuID == "" || stuVeri == "") {
                    swal("错误！", "您的输入为空", "error");
                } else {
                    const res = await fetch('/list/checkOut', {
                		method: 'POST',
                		body: JSON.stringify({ token, stuID, stuVeri }),
                		headers: {
                			'Content-Type': 'application/json'
                		}
                	});
                	
                	const json = await res.json();
    				console.log(json);
    				const { success, error, stuName } = json;
    				
    				if (success) {
    				    swal("绑定信息正确", "一同选宿舍学生姓名：" + stuName + ":)", "success");
    				} else {
    				    if (error == 'NOTIN') {
    				        swal("错误！", "所输入学号不正确！X(", "error");
    				    } else if (error == 'VERIERR') {
    				        swal("错误！", "所输入邀请码不正确！X(", "error");
    				    } else if (error == 'CANERR') {
    				        swal("错误！", "该学生无法选宿舍！X(", "error");
    				    } else if (error == 'SEXERR') {
    				        swal("错误！", "该学生性别不匹配！X(", "error");
    				    } else if (error == 'SAMEERR') {
    				        swal("错误！", "无法邀请自己！X(", "error");
    				    } else if (error == 'ERROR') {
    				        swal("错误！", "后台未知错误产生！请及时联系管理员！X(", "error");
    				    } else if (error == 'SQlERR') {
    				        swal("错误！", "后台数据库产生错误！请及时联系管理员！X(", "error");
    				    } else if (error == 'CHECERR') {
    				        swal("错误！", "已选宿舍的用户无法再进行查询！X(", "error");
    				    } else {
    				        swal("错误！", "未知错误产生！请及时联系管理员！X(", "error");
    				    }
    				}
                }
            }
            
            async function check_2() {
                var token = localStorage.getItem("token");
                const stuID = document.getElementById("stuID_2").value;
                const stuVeri = document.getElementById("stuVeri_2").value;
                console.log(stuID, stuVeri);
                
                if (stuID == "" || stuVeri == "") {
                    swal("错误！", "您的输入为空", "error");
                } else {
                    const res = await fetch('/list/checkOut', {
                		method: 'POST',
                		body: JSON.stringify({ token, stuID, stuVeri }),
                		headers: {
                			'Content-Type': 'application/json'
                		}
                	});
                	
                	const json = await res.json();
    				console.log(json);
    				const { success, error, stuName } = json;
    				
    				if (success) {
    				    swal("绑定信息正确", "一同选宿舍学生姓名：" + stuName + ":)", "success");
    				} else {
    				    if (error == 'NOTIN') {
    				        swal("错误！", "所输入学号不正确！X(", "error");
    				    } else if (error == 'VERIERR') {
    				        swal("错误！", "所输入邀请码不正确！X(", "error");
    				    } else if (error == 'CANERR') {
    				        swal("错误！", "该学生无法选宿舍！X(", "error");
    				    } else if (error == 'SEXERR') {
    				        swal("错误！", "该学生性别不匹配！X(", "error");
    				    } else if (error == 'SAMEERR') {
    				        swal("错误！", "无法邀请自己！X(", "error");
    				    } else if (error == 'ERROR'){
    				        swal("错误！", "后台未知错误产生！请及时联系管理员！X(", "error");
    				    } else {
    				        swal("错误！", "未知错误产生！请及时联系管理员！X(", "error");
    				    }
    				}
                }
            }
            
            async function check_3() {
                var token = localStorage.getItem("token");
                const stuID = document.getElementById("stuID_3").value;
                const stuVeri = document.getElementById("stuVeri_3").value;
                console.log(stuID, stuVeri);
                
                if (stuID == "" || stuVeri == "") {
                    swal("错误！", "您的输入为空", "error");
                } else {
                    const res = await fetch('/list/checkOut', {
                		method: 'POST',
                		body: JSON.stringify({ token, stuID, stuVeri }),
                		headers: {
                			'Content-Type': 'application/json'
                		}
                	});
                	
                	const json = await res.json();
    				console.log(json);
    				const { success, error, stuName } = json;
    				
    				if (success) {
    				    swal("绑定信息正确", "一同选宿舍学生姓名：" + stuName + ":)", "success");
    				} else {
    				    if (error == 'NOTIN') {
    				        swal("错误！", "所输入学号不正确！X(", "error");
    				    } else if (error == 'VERIERR') {
    				        swal("错误！", "所输入邀请码不正确！X(", "error");
    				    } else if (error == 'CANERR') {
    				        swal("错误！", "该学生无法选宿舍！X(", "error");
    				    } else if (error == 'SEXERR') {
    				        swal("错误！", "该学生性别不匹配！X(", "error");
    				    } else if (error == 'SAMEERR') {
    				        swal("错误！", "无法邀请自己！X(", "error");
    				    } else if (error == 'ERROR'){
    				        swal("错误！", "后台未知错误产生！请及时联系管理员！X(", "error");
    				    } else {
    				        swal("错误！", "未知错误产生！请及时联系管理员！X(", "error");
    				    }
    				}
                }
            }
        </script>
        
        <!-- ================================================
        jQuery Library
        ================================================ -->
        <script type="text/javascript" src="../static/js/jquery.min.js"></script>
        
        <!-- ================================================
        Bootstrap Core JavaScript File
        ================================================ -->
        <script src="../static/js/bootstrap/bootstrap.min.js"></script>
        
        <!-- ================================================
        Plugin.js - Some Specific JS codes for Plugin Settings
        ================================================ -->
        <script type="text/javascript" src="../static/js/plugins.js"></script>
        
        <!-- ================================================
        Sweet Alert
        ================================================ -->
        <script src="../static/js/sweet-alert/sweet-alert.min.js"></script>
        
        <!-- ================================================
        Modern Alert
        ================================================ -->
        <script src="../static/js/Modern-alert/main.js"></script>
        
    </body>
    
</html>